package com.cskaoyan.market.service.admin.impl;

import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.MarketOrderGoodsMapper;
import com.cskaoyan.market.db.mapper.MarketOrderMapper;
import com.cskaoyan.market.db.mapper.MarketUserMapper;
import com.cskaoyan.market.service.admin.MarketOrderService;
import com.cskaoyan.market.util.MarketConstant;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Service
public class MarketOrderServiceImpl implements MarketOrderService {

    @Autowired
    MarketOrderMapper orderMapper;
    @Autowired
    MarketUserMapper userMapper;
    @Autowired
    MarketOrderGoodsMapper orderGoodsMapper;

    @Override
    public List<MarketOrder> getMarketOrderList(String[] statusString, String userId, String id, String orderSn, String startTime, String endTime,String sort , String order,Integer page , Integer limit) {

        MarketOrderExample marketOrderExample = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = marketOrderExample.or().andDeletedEqualTo(false);
        // 用户id筛选
        if (userId != null) {
            criteria.andUserIdEqualTo(Integer.valueOf(userId));
        }
        // 订单id筛选
        if (id != null) {
            criteria.andIdEqualTo(Integer.valueOf(id));
        }
        // 订单编号筛选
        if (orderSn != null) {
            criteria.andOrderSnEqualTo(orderSn);
        }
        // 起止时间筛选
        if (!"".equals(startTime) && !"".equals(endTime)) {
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime start = LocalDateTime.parse(startTime, dateTimeFormatter);
            LocalDateTime end = LocalDateTime.parse(endTime, dateTimeFormatter);
            criteria.andAddTimeBetween(start, end);
        }
        // 订单状态筛选
        if (statusString != null) {
            List<Short> statusArrays = new ArrayList<>();
            for (String s : statusString) {
                statusArrays.add(Short.valueOf(s));
            }
            criteria.andOrderStatusIn(statusArrays);
        }
        marketOrderExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(page, limit);
        List<MarketOrder> marketOrders = orderMapper.selectByExample(marketOrderExample);

        return marketOrders;
    }

    @Override
    public MarketOrder getMarketOrder(String orderId) {

        MarketOrderExample marketOrderExample = new MarketOrderExample();
        marketOrderExample.or().andIdEqualTo(Integer.valueOf(orderId)).andDeletedEqualTo(false);
        MarketOrder marketOrder = orderMapper.selectOneByExample(marketOrderExample);

        return marketOrder;
    }

    @Override
    public int getUserIdByOrderId(String orderId) {

        MarketOrderExample marketOrderExample = new MarketOrderExample();
        marketOrderExample.or().andIdEqualTo(Integer.valueOf(orderId)).andDeletedEqualTo(false);
        MarketOrder marketOrder = orderMapper.selectOneByExampleSelective(marketOrderExample, MarketOrder.Column.userId);
        // 返回查询到的用户id
        return marketOrder.getUserId();
    }

    @Override
    public MarketUser getUserByOrderId(int userId) {
        MarketUserExample marketUserExample = new MarketUserExample();
        marketUserExample.or().andIdEqualTo(userId).andDeletedEqualTo(false);
        MarketUser marketUser = userMapper.selectOneByExampleSelective(marketUserExample, MarketUser.Column.nickname, MarketUser.Column.avatar);
        return marketUser;
    }

    @Override
    public List<MarketOrderGoods> getMarketOrderGoods(String orderId) {
        MarketOrderGoodsExample orderGoodsExample = new MarketOrderGoodsExample();
        orderGoodsExample.or().andOrderIdEqualTo(Integer.valueOf(orderId)).andDeletedEqualTo(false);
        List<MarketOrderGoods> orderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);
        return orderGoods;
    }

    @Override
    public int refund(Integer orderId, BigDecimal refundAmount) {

        MarketOrder marketOrder = new MarketOrder();
        // 后续只需要在这里添加就可以
        // 退款功能,通过orderId找到相应的order表把表中订单状态修改为  203: '已退款'
        marketOrder.setRefundAmount(refundAmount);
        marketOrder.setOrderStatus(MarketConstant.HAVE_REFUND);
        marketOrder.setRefundType("微信退款接口");
        marketOrder.setRefundTime(LocalDateTime.now());
        marketOrder.setUpdateTime(LocalDateTime.now());

        MarketOrderExample marketOrderExample = new MarketOrderExample();
        marketOrderExample.or().andIdEqualTo(orderId);

        int row = orderMapper.updateByExampleSelective(marketOrder, marketOrderExample);
        return row;
    }

    @Override
    public int deleteOrder(Integer orderId) {
        // 此处进行逻辑删除
        MarketOrder marketOrder = new MarketOrder();
        // 逻辑删除
        marketOrder.setDeleted(true);

        MarketOrderExample marketOrderExample = new MarketOrderExample();
        marketOrderExample.or().andIdEqualTo(orderId).andDeletedEqualTo(false);
        int row = orderMapper.updateByExampleSelective(marketOrder, marketOrderExample);

        return row;
    }

    @Override
    public int shipOrder(Integer orderId, String shipChannel, String shipSn) {

        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setShipChannel(shipChannel);
        marketOrder.setShipSn(shipSn);
        // 修改订单状态从已付款201 到已发货301
        marketOrder.setOrderStatus(MarketConstant.HAVE_SHIPMENT);
        // 获取当前时间，作为发货时间
        LocalDateTime shipTime = LocalDateTime.now();
        marketOrder.setShipTime(shipTime);

        MarketOrderExample marketOrderExample = new MarketOrderExample();
        marketOrderExample.or().andIdEqualTo(orderId).andDeletedEqualTo(false);
        int row = orderMapper.updateByExampleSelective(marketOrder, marketOrderExample);

        return row;
    }
}
